/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.DBExecutor;
import net.iwsh.core.db.ext.DialectSyntaxParser;
import net.iwsh.core.db.ext.SQLWithParams;
import net.iwsh.core.utils.IDGenerator;

import com.us.usws.model.domain.system.UserAttr;
import com.us.usws.model.flat.system.DOUserAttr;

/**
 * DAO class
 * @author robot
 */
public class UserAttrDAO {

	/**
	 * Query SQLs
	 */
	private static final String SQL_GET_TOTAL_COUNT = "select count(*) from sys_user_attr";

	private static final String SQL_GET_ALL_USERATTRS = "select * from sys_user_attr";

	private static final String SQL_GET_USERATTR_BY_PK = "select * from sys_user_attr where uid = ?";

	/**
	 * CUR SQLs
	 */
	private static final String SQL_INSERT_USERATTR = "insert into sys_user_attr(uid,province_code,city_code,strict_code,mobile_no,contact_addr,birth_date,qq_no) values (?,?,?,?,?,?,?,?)";

	private static final String SQL_UPDATE_USERATTR = "update sys_user_attr set province_code=?, city_code=?, strict_code=?, mobile_no=?, contact_addr=?, birth_date=?, qq_no=? where uid=?";

	private static final String SQL_DEL_USERATTR = "delete from sys_user_attr where uid=?";

	/**
	 * DBExeutor
	 */
	private DBExecutor dbe = new DBExecutor();

	/**
	 * Get Count.
	 */
	public int getTotalCount() {
		return dbe.getCount(SQL_GET_TOTAL_COUNT);
	}

	/**
	 * Get all list.
	 */
	public List<UserAttr> getAllUserAttrs() {
		return dbe.list(SQL_GET_ALL_USERATTRS, UserAttr.class);
	}

	/**
	 * Get all list by pagination.
	 */
	public List<UserAttr> getUserAttrs(int start, int end) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_ALL_USERATTRS, null, start, end);
		return dbe.list(swp.getSql(), UserAttr.class, swp.getParams());
	}

	/**
	 * Get UserAttr by primary key.
	 */
	public UserAttr getUserAttr(String uid) {
		UserAttr u = dbe.load(SQL_GET_USERATTR_BY_PK, UserAttr.class, uid);
		return u;
	}

	/**
	 * Insert one row.
	 */
	public String insertUserAttr(DOUserAttr userAttr) {
		String uid = IDGenerator.getInstance().genUID();
		userAttr.setUid(uid);
		dbe.execute(SQL_INSERT_USERATTR, userAttr.getUid(), userAttr.getProvinceCode(), userAttr.getCityCode(), userAttr.getStrictCode(),
				userAttr.getMobileNo(), userAttr.getContactAddr(), userAttr.getBirthDate(), userAttr.getQqNo());
		return uid;
	}

	/**
	 * Update by PK.
	 */
	public void updateUserAttr(DOUserAttr userAttr) {
		dbe.execute(SQL_UPDATE_USERATTR, userAttr.getProvinceCode(), userAttr.getCityCode(), userAttr.getStrictCode(),
				userAttr.getMobileNo(), userAttr.getContactAddr(), userAttr.getBirthDate(), userAttr.getQqNo(), userAttr.getUid());
	}

	/**
	 * Delete row by PK.
	 */
	public void deleteUserAttr(String uid) {
		dbe.execute(SQL_DEL_USERATTR, uid);
	}

}